Central Article

Creating Offline Web Applications in Macromedia Central


Table of Contents

    1. Introduction
    2. How Central Uses Connection Awareness
    3. Caching Files and Data on the Client Machine
    4. Building a Central Application
    5. Implementing Network Status
    6. Adding Functionality with Application Controls
    7. Final Initiation
    8. Testing Your Central Application

Caching Files and Data on the Client Machine

Caching files and data in Macromedia Central is a large part of making your applications work offline. There are three methods to caching files for offline use.

You can include arbitrary files that Central will download when users install your application, and you can use local shared objects. Macromedia Central also includes APIs that let you simply specify a URL of a file to store on your users’ local machines. After you've cached data locally with any of these methods, Flash Player in Central looks in the cache first to retrieve data with any of the ActionScript commands for retrieving remote data, such as the loadMovie and loadVariables commands. You can cache most file types, but there are some files that are considered unsafe that you cannot cache on users’ machines. These file types include: .ad, .hlp, .msi, .vb, .adp, .hta, .msp, .vbe, .asp, .inf, .mst, .vbs, .bas, .ins, .pcd, .vsd, .bat, .isp, .pif, .vss, .chm, .jsp, .reg, .vst, .cmd, .jse, .scr, .vsw, .com, .lnk, .sct, .ws, .cpl, .mdb, .shb, .wsc, .crt, .mde, .shs, .wsf, .exe, .msc, .url, and .wsh.

For security reasons, you cannot use getURL to open cached local Internet files. The getURL calls will try to open users' default browsers and download the file from the web server. You may instruct users on the location of the cached files so they can open and execute them, but this method is not recommended. In its current iteration, use caching for files that Flash Player can use and or display.

Central stores cached files in the following locations:

  • Windows 2000 and XP: C:\Documents & Settings\<userName>\Application Data\ Macromedia\Central\Local Internet\<domainName.com>
  • Windows Win 98 and ME: C:\WINDOWS\Application Data\Macromedia\Central\ Local Internet\<domainName.com>
  • Macintosh: HardDisk:Users: <userName>:Library:Application Support:Macromedia:Central: Local Internet:<domainName.com>

The product.xml file and file Tag

The product.xml file is the XML descriptor file for your product. The product.xml file describes your product, including all of the applications and their associated components, pods, and agents. Find more information about the product.xml file in the Macromedia Central SDK.

Use a child element of an Application tag, the file tag, to specify additional files that Central will install along with your application. Installing files locally gives your application faster access to the files and lets users access the files when Central is offline. This tag stores files under the Central directory, along with any of the other essential application files. The file tag has a src attribute that specifies the source of the cached file. The src attribute can either be an absolute or relative URL.

<file src="/directory/myImage.jpg"/>	
<file src="http://www.domain.com/directory/myStrings.txt"/>

When you cache files, they should be downloaded from the same domain as the application, otherwise the user will receive an error and the application will not install. Macromedia Central cannot discern separate hosts within the same domain. For example; http://www.domain.com/file.swf would overwrite http://sub.domain.com/file.swf.

Note: The size limit of a cached file is 20MB. During your application installation, if one or more of the files are over 20MB, Central will not download the oversized file, and the user will receive an error indicating some files did not download.

The addToLocalInternetCache Method

New in Macromedia Central is the ability to use built-in APIs for caching data for offline use. There are three methods for managing cached files on users’ machines: addToLocalInternetCache, inLocalInternetCache, and removeFromLocalInternetCache. These methods are all available within the scope of the AgentManager, Shell, or Console.

Use addToLocalInternetCache(arguments are: strUrl, bOverwrite, and expiration) to add data to the local cache. The strUrl argument specifies the absolute URL of the file that you wish to cache. The other arguments are optional and help you manage the updates and duration the files in cache. The bOverwrite argument is a boolean value that specifies true if data from the URL can overwrite the cached data; or false, if the data is already in the cache and the data from the URL cannot overwrite the file. The expiration argument can be either a date object (specifying a date when Central will remove the file from the local cache) or a number (specifying how many days the file will stay in cache). No special techniques are necessary to access cached data. It is important to realize that these types of methods will always retrieve data from the cache if it is available. To get new data from the Internet, you must either delete the data from the cache with the removeFromLocalInternetCache(strUrl) method, which removes the specified data out of the cache, or replace it by setting the bOverwrite parameter of the addToLocalInternetCache method to true.

I recommend using inLocalInternetCache(strUrl) to check whether a prior call to addToLocalInternetCache was successful and whether a URL's asset is currently in the cache. This method returns a true or false.

Use ObjectName.removeFromLocalInternetCache(strUrl) to remove a URL's asset from the local Internet cache. For example:

	/* this example adds a jpg to the cache, loads it, then checks if successful */
onActivate=function(shell)
{
     /* set a variable to reference the AgentManager, Shell, or Console */
     gShell=shell;

     var theFile="http://www.mysite.com/images/photo.jpg"
     // add it to the cache
     gShell.addToLocalInternetCache(theFile);
     someClipInstance.loadMovie(theFile);

     // check that the disk quota wasn't exceeded
     if(gShell.inLocalInternetCache(theFile) != true)
     {
          /* trigger homemade function to explain the image wasn't downloaded */
          myAlertFunction("The photo won't be available when offline");
     }

};

Note: In Central Preferences, the user sets the size limit for the cache that stores data. The default size is 20MB and all Central applications share that space. The expiration argument is optional and can be either a date object or an integer. If you specify this argument, Central removes cached data from the cache on the date specified in a date object or after the number of days specified by an integer. Also, when a user uninstalls or updates an application, Central removes any cache that the application created.

Local Shared Objects

Since Macromedia Central is built on top of Flash Player 6, it can take advantage of local shared objects just as any recently published Flash movie can. Shared objects are useful when storing results from web services, user preferences, or other parsed data. To ensure that your objects have unique names, include your application's unique application ID in the name of the local shared object. When Central calls its onActivate event handler, it passes the unique ID to your application.

	Example:

	function onActivate(shell, id)
	{
   		theName = "sharedObj" + id;
   		_so = SharedObject.getLocal(theName)
	}

Macromedia Central does not remove local shared objects from the user's machine when the user uninstalls an application. It is up to the developer to clean out any local shared objects for an application when a user uninstalls the application. Central, based on the Flash Player 6, does not have the SharedObject.clear method available to clear local shared objects like Flash Player 7. You can overcome this by implementing the following code, which removes the local shared object from the users' machines when they uninstall the application.

  Example:
    function onUninstall()
    {
        for(var value in _so.data)
           {
             delete _so.data[value];
           }
    }

One final note about local shared objects in Macromedia Central: Not all elements of your movie will be from the same domain. When a user wants to adjust the local storage settings for your application or if you use the System.showSettings function to display these settings, an undesired settings dialog message might appear for the user. If the current focus of the Central application is on an element that is from the macromedia.com domain like the Central components, for example, the settings dialog will display information for that domain and not the domain of your application. To fix this issue, set the focus to one of the elements of your application that is from your domain before a request, to show the settings dialog is made.

Example:
    function onSomeEvent() 
    {
         Selection.setFocus(this);
         System.showSettings(1);
    }